import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', {
  state: () => {
    return {
      isMobile: false,
      // 博主信息
      bloggerInfo: {
        name: '迷之de小孩灬',
      },
      highLightIndex: 1,
      typeIndex: 0,
      typeList: []
    }
  },
  actions: {
    setIsMobile(val) {
      this.isMobile = val
    },
    setHighLightIndex(val) {
      this.highLightIndex = val
    },
    setTypeIndex(val) {
      this.typeIndex = val
    },
    async getTypeList(app) {
      try {
        const res = await app.$API.type.all()
        const typeList = res.data.map(v => {
          const { name, id, articleNum } = v
          return {
            name,
            value: id,
            number: articleNum
          }
        })
        if (typeList.length) {
          const sum = typeList.reduce((prev, item) => {
            return prev += item.number
          }, 0)
          this.typeList = [{
            name: '全部', value: '', number: sum
          },
          ...typeList]
        }
      } catch (error) {

      }
    }
  },
})
